package api

import (
	goods "api-gateway/basic/goods_proto"
	"api-gateway/basic/inits"
	"api-gateway/handler/request"
	"api-gateway/handler/response"
	"github.com/gin-gonic/gin"
)

// SetInShopCar
// @Summary      加入购物车
// @Description  加入购物车
// @Tags         购物车模块
// @Accept       json
// @Produce      json
// @Param        shopCar  body     request.SetInShopCarReq  true  "购物车参数"
// @Success      200  {object}  response.Response
// @Failure      400   {object}  response.Response
// @Security TokenAuth
// @Router       /shopCar/setInShopCar [post]
func SetInShopCar(c *gin.Context) {
	userId := c.GetUint("userId")
	var req request.SetInShopCarReq
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}
	_, err := inits.GoodsClient.AddShopCar(c, &goods.AddShopCarReq{
		UserId:  int64(userId),
		GoodsId: req.GoodsId,
		Num:     req.Num,
	})
	if err != nil {
		response.Error(c, 500, "服务端错误", err.Error())
		return
	}
	response.Success(c, "添加购物车成功", nil)
}

// AddShopCarNum
// @Summary      增加购物车商品数量 （+1）
// @Description  增加购物车商品数量 （+1）
// @Tags         购物车模块
// @Accept       json
// @Produce      json
// @Param        shopCar  body      request.GetGoodsAndUserIdReq  true  "购物车参数"
// @Success      200  {object}  response.Response
// @Failure      400   {object}  response.Response
// @Security TokenAuth
// @Router       /shopCar/addShopCarNum [post]
func AddShopCarNum(c *gin.Context) {
	userId := c.GetUint("userId")
	var req request.GetGoodsAndUserIdReq
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}
	_, err := inits.GoodsClient.AddShopCarNum(c, &goods.AddShopCarNumReq{
		UserId:  int64(userId),
		GoodsId: req.GoodsId,
	})
	if err != nil {
		response.Error(c, 500, "服务端错误", err.Error())
		return
	}
	response.Success(c, "添加数量成功", nil)
}

// PopShopCarNum
// @Summary      扣减购物车商品数量 （-1）
// @Description  扣减购物车商品数量 （-1）
// @Tags         购物车模块
// @Accept       json
// @Produce      json
// @Param        shopCar  body      request.GetGoodsAndUserIdReq  true  "购物车参数"
// @Success      200  {object}  response.Response
// @Failure      400   {object}  response.Response
// @Security TokenAuth
// @Router       /shopCar/popShopCarNum [post]
func PopShopCarNum(c *gin.Context) {
	userId := c.GetUint("userId")
	var req request.GetGoodsAndUserIdReq
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}
	_, err := inits.GoodsClient.PopShopCarNum(c, &goods.PopShopCarNumReq{
		UserId:  int64(userId),
		GoodsId: req.GoodsId,
	})
	if err != nil {
		response.Error(c, 500, "服务端错误", err.Error())
		return
	}
	response.Success(c, "扣减数量成功", nil)
}

// UpdateShopCarNum
// @Summary      直接修改购物车商品数量
// @Description  直接修改购物车商品数量
// @Tags         购物车模块
// @Accept       json
// @Produce      json
// @Param        shopCar  body   request.SetInShopCarReq  true  "购物车参数"
// @Success      200  {object}  response.Response
// @Failure      400   {object}  response.Response
// @Security TokenAuth
// @Router       /shopCar/updateShopCarNum [post]
func UpdateShopCarNum(c *gin.Context) {
	userId := c.GetUint("userId")
	var req request.SetInShopCarReq
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}
	_, err := inits.GoodsClient.UpdateShopCarNum(c, &goods.UpdateShopCarNumReq{
		UserId:  int64(userId),
		GoodsId: req.GoodsId,
		Num:     req.Num,
	})
	if err != nil {
		response.Error(c, 500, "服务端错误", err.Error())
		return
	}
	response.Success(c, "修改数量成功", nil)
}

// GetShopCarAllPrice
// @Summary      获取购物车所有商品总价
// @Description  获取购物车所有商品总价
// @Tags         购物车模块
// @Accept       json
// @Produce      json
// @Param        shopCar  query   request.GetUserId  true  "购物车参数"
// @Success      200  {object}  response.Response
// @Failure      400   {object}  response.Response
// @Security TokenAuth
// @Router       /shopCar/getShopCarAllPrice [get]
func GetShopCarAllPrice(c *gin.Context) {
	var req request.GetUserId
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}
	res, err := inits.GoodsClient.GetShopCarAllPrice(c, &goods.GetShopCarAllPriceReq{UserId: req.UserId})
	if err != nil {
		response.Error(c, 500, "服务端错误", err.Error())
		return
	}
	response.Success(c, "获取成功", res)
}

// ShopCarList
// @Summary      购物车商品列表
// @Description  购物车商品列表
// @Tags         购物车模块
// @Accept       json
// @Produce      json
// @Param        shopCar  query   request.GetUserId  true  "购物车参数"
// @Success      200  {object}  response.Response
// @Failure      400   {object}  response.Response
// @Security TokenAuth
// @Router       /shopCar/shopCarList [get]
func ShopCarList(c *gin.Context) {
	var req request.GetUserId
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}
	res, err := inits.GoodsClient.UserShopCarList(c, &goods.UserShopCarListReq{UserId: req.UserId})
	if err != nil {
		response.Error(c, 500, "服务端错误", err.Error())
		return
	}
	response.Success(c, "获取成功", res)
}

// CleanShopCar
// @Summary      清空购物车
// @Description  清空购物车
// @Tags         购物车模块
// @Accept       json
// @Produce      json
// @Param        shopCar  body   request.GetUserId  true  "购物车参数"
// @Success      200  {object}  response.Response
// @Failure      400   {object}  response.Response
// @Security TokenAuth
// @Router       /shopCar/cleanShopCar [get]
func CleanShopCar(c *gin.Context) {
	var req request.GetUserId
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}
	_, err := inits.GoodsClient.CleanShopCar(c, &goods.CleanShopCarReq{UserId: req.UserId})
	if err != nil {
		response.Error(c, 500, "服务端错误", err.Error())
		return
	}
	response.Success(c, "清空成功", nil)
}

// UserSearchShopCarByName
// @Summary      查询购物车收藏的商品 -
// @Description  查询购物车收藏的商品 -
// @Tags         购物车模块
// @Accept       json
// @Produce      json
// @Param        shopCar  query   request.SearchInShopCar  true  "购物车参数"
// @Success      200  {object}  response.Response
// @Failure      400   {object}  response.Response
// @Security TokenAuth
// @Router       /shopCar/userSearchShopCarByName [get]
func UserSearchShopCarByName(c *gin.Context) {
	userId := c.GetUint("userId")
	var req request.SearchInShopCar
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}
	res, err := inits.GoodsClient.UserSearchShopCarByName(c, &goods.UserSearchShopCarByNameReq{
		Name:   req.Name,
		UserId: int64(userId),
	})
	if err != nil {
		response.Error(c, 500, "服务端错误", err.Error())
		return
	}
	response.Success(c, "查询成功", res)
}

// DelOneShopCar
// @Summary      删除某 redis 数据 -
// @Description  删除某 redis 数据 -
// @Tags         购物车模块
// @Accept       json
// @Produce      json
// @Param        shopCar  body   request.GetGoodsAndUserIdReq  true  "购物车参数"
// @Success      200  {object}  response.Response
// @Failure      400   {object}  response.Response
// @Security TokenAuth
// @Router       /shopCar/delOneShopCar [post]
func DelOneShopCar(c *gin.Context) {
	userId := c.GetUint("userId")
	var req request.GetGoodsAndUserIdReq
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}
	_, err := inits.GoodsClient.DelOneShopCarData(c, &goods.DelOneShopCarDataReq{
		UserId:  int64(userId),
		GoodsId: req.GoodsId,
	})
	if err != nil {
		response.Error(c, 500, "服务端错误", err.Error())
		return
	}
	response.Success(c, "删除成功", nil)
}
